from otree.api import *

doc = """
投资者行为实验
"""


class C(BaseConstants):
    NAME_IN_URL = 'gf'
    PLAYERS_PER_GROUP = None
    NUM_ROUNDS = 3
    GF_RATIO = None
    TABLE_STYLE = """
<style>
img {
  width: auto;
  height: 300px;
}
  /* 设置整个表格的样式 */
  table {
    width: 100%; /* 表格宽度 */
    border-collapse: collapse; /* 合并边框 */
    margin: 50px 0; /* 外边距 */
    font-size: 0.9em; /* 字体大小 */
    font-family: sans-serif; /* 字体 */
    min-width: 400px; /* 最小宽度 */
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.15); /* 阴影效果 */
  }

  /* 设置表头的样式 */
  th {
    background-color: #4286f4; /* 背景颜色 */
    color: white; /* 文字颜色 */
    text-align: left; /* 文字对齐 */
    padding: 12px 15px; /* 内边距 */
  }

  /* 设置表格标题的样式 */
  caption {
    padding: 10px; /* 内边距 */
    caption-side: bottom; /* 标题位置 */
    color: #888; /* 文字颜色 */
    font-size: 1em; /* 字体大小 */
    margin-top: 12px; /* 上边距 */
  }

  /* 设置单元格的样式 */
  td {
    background-color: #f7f7f7; /* 背景颜色 */
    color: #333; /* 文字颜色 */
    text-align: left; /* 文字对齐 */
    padding: 12px 15px; /* 内边距 */
  }

  /* 设置奇数行的背景颜色 */
  tr:nth-child(even) {
    background-color: #e7e7e7;
  }

  /* 设置鼠标悬停时的行样式 */
  tr:hover {
    background-color: #f1f1f1;
  }

  /* 设置表格边框 */
  th, td {
    border: 1px solid #ddd; /* 边框颜色 */
  }
</style>
    """


TREND_UP, TREND_DOWN = '上涨', '下跌'
YES, NO = "是", "否"
LABEL_PIC_CURRENT_TREND = '请对图片中股票走势进行判断'
LABEL_PIC_NEXT_TREND = '并判断下个交易日股票涨跌情况'
LABEL_PIC_NEXT_TREND2 = '请再次判断下个交易日股票涨跌情况'


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):
    sum_gf_up_ds_1 = models.IntegerField()
    sum_gf_down_ds_1 = models.IntegerField()

    sum_gf_up_us_1 = models.IntegerField()
    sum_gf_down_us_1 = models.IntegerField()

    sum_gf_up_dl_1 = models.IntegerField()
    sum_gf_down_dl_1 = models.IntegerField()

    sum_gf_up_ul_1 = models.IntegerField()
    sum_gf_down_ul_1 = models.IntegerField()
    gf_ratio = models.IntegerField()


class Player(BasePlayer):
    gf_ds = models.StringField(choices=[TREND_UP, TREND_DOWN], label=LABEL_PIC_CURRENT_TREND,
                               widget=widgets.RadioSelectHorizontal)

    gf_ds_1 = models.StringField(choices=[TREND_UP, TREND_DOWN], label=LABEL_PIC_NEXT_TREND,
                                 widget=widgets.RadioSelectHorizontal)

    gf_ds_2 = models.StringField(choices=[TREND_UP, TREND_DOWN], label=LABEL_PIC_NEXT_TREND2,
                                 widget=widgets.RadioSelectHorizontal)

    gf_us = models.StringField(choices=[TREND_UP, TREND_DOWN], label=LABEL_PIC_CURRENT_TREND,
                               widget=widgets.RadioSelectHorizontal)

    gf_us_1 = models.StringField(choices=[TREND_UP, TREND_DOWN], label=LABEL_PIC_NEXT_TREND,
                                 widget=widgets.RadioSelectHorizontal)

    gf_us_2 = models.StringField(choices=[TREND_UP, TREND_DOWN], label=LABEL_PIC_NEXT_TREND2,
                                 widget=widgets.RadioSelectHorizontal)

    gf_dl = models.StringField(choices=[TREND_UP, TREND_DOWN], label=LABEL_PIC_CURRENT_TREND,
                               widget=widgets.RadioSelectHorizontal)

    gf_dl_1 = models.StringField(choices=[TREND_UP, TREND_DOWN], label=LABEL_PIC_NEXT_TREND,
                                 widget=widgets.RadioSelectHorizontal)

    gf_dl_2 = models.StringField(choices=[TREND_UP, TREND_DOWN], label=LABEL_PIC_NEXT_TREND2,
                                 widget=widgets.RadioSelectHorizontal)

    gf_ul = models.StringField(choices=[TREND_UP, TREND_DOWN], label=LABEL_PIC_CURRENT_TREND,
                               widget=widgets.RadioSelectHorizontal)

    gf_ul_1 = models.StringField(choices=[TREND_UP, TREND_DOWN], label=LABEL_PIC_NEXT_TREND,
                                 widget=widgets.RadioSelectHorizontal)

    gf_ul_2 = models.StringField(choices=[TREND_UP, TREND_DOWN], label=LABEL_PIC_NEXT_TREND2,
                                 widget=widgets.RadioSelectHorizontal)


def count_with_ratio(num_players, ratio):
    return int((num_players - 1) * ratio / 100.0 + 0.5)


def wait_page_ds_1(group: Group):
    players = group.get_players()
    ratio = group.session.config['GF_RATIO']
    if ratio and 0 < ratio:
        group.gf_ratio = ratio
        group.sum_gf_up_ds_1 = count_with_ratio(len(players), ratio)
        group.sum_gf_down_ds_1 = len(players) - group.sum_gf_up_ds_1
    else:
        group.sum_gf_up_ds_1 = sum(p.gf_ds_1 == TREND_UP for p in players)
        group.sum_gf_down_ds_1 = sum(p.gf_ds_1 == TREND_DOWN for p in players)


def wait_page_us_1(group: Group):
    players = group.get_players()
    ratio = group.session.config['GF_RATIO']
    if ratio and 0 < ratio:
        group.gf_ratio = ratio
        group.sum_gf_down_us_1 = count_with_ratio(len(players), ratio)
        group.sum_gf_up_us_1 = len(players) - group.sum_gf_down_us_1
    else:
        group.sum_gf_up_us_1 = sum(p.gf_us_1 == TREND_UP for p in players)
        group.sum_gf_down_us_1 = sum(p.gf_us_1 == TREND_DOWN for p in players)


def wait_page_dl_1(group: Group):
    players = group.get_players()
    ratio = group.session.config['GF_RATIO']
    if ratio and 0 < ratio:
        group.gf_ratio = ratio
        group.sum_gf_up_dl_1 = count_with_ratio(len(players), ratio)
        group.sum_gf_down_dl_1 = len(players) - group.sum_gf_up_dl_1
    else:
        group.sum_gf_up_dl_1 = sum(p.gf_dl_1 == TREND_UP for p in players)
        group.sum_gf_down_dl_1 = sum(p.gf_dl_1 == TREND_DOWN for p in players)


def wait_page_ul_1(group: Group):
    players = group.get_players()
    ratio = group.session.config['GF_RATIO']
    if ratio and 0 < ratio:
        group.gf_ratio = ratio
        group.sum_gf_down_ul_1 = count_with_ratio(len(players), ratio)
        group.sum_gf_up_ul_1 = len(players) - group.sum_gf_down_ul_1
    else:
        group.sum_gf_up_ul_1 = sum(p.gf_ul_1 == TREND_UP for p in players)
        group.sum_gf_down_ul_1 = sum(p.gf_ul_1 == TREND_DOWN for p in players)


# PAGES


class GfDS1(Page):
    form_model = 'player'
    form_fields = ['gf_ds', 'gf_ds_1']


class GfDS1Resp(Page):
    pass


class GfDL1Resp(Page):
    pass


class GfUS1Resp(Page):
    pass


class GfUL1Resp(Page):
    pass


class GfDS2(Page):
    form_model = 'player'
    form_fields = ['gf_ds_2']


class WaitDS1(WaitPage):
    after_all_players_arrive = wait_page_ds_1


class GfUS1(Page):
    form_model = 'player'
    form_fields = ['gf_us', 'gf_us_1']


class GfUS2(Page):
    form_model = 'player'
    form_fields = ['gf_us_2']


class GfUS3(Page):
    pass


class WaitUS1(WaitPage):
    after_all_players_arrive = wait_page_us_1


class GfDL1(Page):
    form_model = 'player'
    form_fields = ['gf_dl', 'gf_dl_1']


class GfDL2(Page):
    form_model = 'player'
    form_fields = ['gf_dl_2']


class GfDL3(Page):
    pass


class WaitDL1(WaitPage):
    after_all_players_arrive = wait_page_dl_1


class GfUL1(Page):
    form_model = 'player'
    form_fields = ['gf_ul', 'gf_ul_1']


class GfUL2(Page):
    form_model = 'player'
    form_fields = ['gf_ul_2']


class WaitUL1(WaitPage):
    after_all_players_arrive = wait_page_ul_1


page_sequence = [
    GfDS1, WaitDS1, GfDS1Resp, GfDS2,
    GfUS1, WaitUS1, GfUS1Resp, GfUS2,
    GfDL1, WaitDL1, GfDL1Resp, GfDL2,
    GfUL1, WaitUL1, GfUL1Resp, GfUL2,
]
